package com.hao.nettyrpc.entity;

import com.hao.nettyrpc.enums.RpcResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author haoge
 * @version 1.0
 * @date 2023/4/10 22:56
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RpcResponse<T> implements Serializable {
    //应答序列号
    private String responseId;
    //响应状态吗
    private Integer statusCode;
    //响应状态补充信息
    private String message;
    //响应数据
    private T data;

    public static <T> RpcResponse<T> success(T data,String responseId) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setResponseId(responseId);
        response.setStatusCode(RpcResponseCode.SUCCESS.getCode());
        response.setMessage(RpcResponseCode.SUCCESS.getMessage());
        response.setData(data);
        return response;
    }

    public static <T> RpcResponse<T> fail(RpcResponseCode code, String responseId) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setResponseId(responseId);
        response.setStatusCode(code.getCode());
        response.setMessage(code.getMessage());
        return response;
    }
}
